package com.demo.servlet.demo1217;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo1217/demo02.do")
public class Demo02Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有 Cookie
        Cookie[] cookies = request.getCookies();
        //遍历所有 Cookie
//        for (int i = 0; i < cookies.length; i++) {
//            Cookie cookie = cookies[i];
//            System.out.println("name = " + cookie.getName());
//            System.out.println("value = " + cookie.getValue());
//        }
        //2.找到 Cookie 再去修改
        Cookie key3 = CookieUtils.findCookie("key3", cookies);
        key3.setValue("value_333");
        response.addCookie(key3);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
